<?php

namespace app\videoReport\logic;

use app\videoReport\model\VideoInput as VideoInputModel;
use think\facade\Db;
use app\data\util\HttpUtil;
use ba\Exception;
use think\facade\Cache;
use app\common\library\Excel;
use Throwable;

class VideoInputLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new VideoInputModel();
    }
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $createtime = $params['createtime'] ?? [];
        $limit = $params['limit'] ?? 10;
        $where = [];

        if (!empty($createtime)) {
            $createtimeArr = explode(',', $createtime);
            $where[] = ['createtime', '>=', strtotime($createtimeArr[0])];
            $where[] = ['createtime', '<=', strtotime($createtimeArr[1])];
        }
        $list = $this->model
            ->field('a.*,b.name as shop_name')
            ->alias('a')
            ->join('ba_shops b','b.id = a.shop_id', 'left')
            ->where($where)
            ->paginate($limit);
        $lists = $list->items();
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }
    

    public function add($data)
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }

        $result = false;
        $this->model->startTrans();
        // try {
            //处理时间
            $data['createtime'] = strtotime($data['createtime']);
            
            $result = $this->model->insert($data);
            
            $this->model->commit();
        // } catch (Throwable $e) {
        //     $this->model->rollback();
        //     return json(DefaultResponse(201, false, $e->getMessage(), null));
        // }
        if ($result !== false) {
            return json(DefaultResponse(200, true, '添加成功!', null));
        } else {
            return json(DefaultResponse(201, false, '添加失败!', null));
        }
    }

    public function edit($project, $data)
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }

        $result = false;
        $this->model->startTrans();
        try {
            //处理时间
            $data['createtime'] = strtotime($data['createtime']);

            $result = $project->save($data);
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return json(DefaultResponse(201, false, $e->getMessage(), null));
        }
        if ($result !== false) {
            return json(DefaultResponse(200, true, '更新成功!', null));
        } else {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
    }

    /**
     * @desc 剪辑统计
     * @date 2024-04-29
     */
    public function getShowcaseTotal($params=[]){
        $shop_id = $params['shop_id'] ?? "";
        $date    = $params['date'] ?? '';
        $name    = $params['name'] ?? '';
        $limit   = $params['limit'] ?? 10;
        $where   = [];
        if ($shop_id) {
            $where[] = ['shop_id', '=', $shop_id];
        }

        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }


        $thismonth = date('m');
        $thisyear  = date('Y');

        if (!$date) {
            $times = get_month_time($thisyear,$thismonth);
            $month_date = $thismonth;
        }else{
            $date  = explode('-',$date);
            $times = get_month_time($date[0],$date[1]);
            $month_date = $date[1];
        }

        $start_time = $times['first'];
        $end_time   = $times['last'];

        $where[] = ['createtime', '>=', $start_time];
        $where[] = ['createtime', '<=', $end_time];

        $res = Db::name('advertising_editinglist')
            ->where($where)
            ->group('name')
            ->field('name, sum(num) as total_num, GROUP_CONCAT(DISTINCT shop_id) as shop_ids')
            ->paginate($limit);
        $list =  $res->items()?:[];
        $list = objectToArray($list);
        foreach($list as $k=>$v){
            $list[$k]['shop_name'] = self::get_shop_name($v['shop_ids']);
            $list[$k]['date']      = $month_date;
        }

        $data = [
            'list'  => $list,
            'total' => $res->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    //获取店铺名称
    private function get_shop_name($ids=''){
        if(!$ids)  return  '';
        $ids = explode(',',$ids);
        $names = Db::name('shops')->whereIn('id',$ids)->column('name');
        return  $names ? implode(',',$names) : '';
    }

    
}
